package org.example.votingsystem.web;

import org.example.votingsystem.account.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.votingsystem.dao.AccountYearPeopleDao;
import org.example.votingsystem.dao.YearPeopleDao;
import org.example.votingsystem.items.YearPeople;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/api/admin")
public class AdminSeverlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String mark = req.getParameter("mark");
        String account = req.getParameter("account");
        String id = req.getParameter("id");
        YearPeopleDao adminDao = new YearPeopleDao();
        AccountYearPeopleDao accountYearPeopleDao = new AccountYearPeopleDao();
        if ("delete".equals(mark)) {
            try {
                adminDao.deleteYearPeople(account);
                Result result = new Result(200, "Delete success", null);
                resp.getWriter().print(new ObjectMapper().writeValueAsString(result));
            } catch (SQLException e) {
                Result result = new Result(500, "Delete failed", null);
                resp.getWriter().print(new ObjectMapper().writeValueAsString(result));
            }
        }else if("getYearPeopleAccount".equals(mark)){
            try {
                ArrayList<YearPeople> yearPeoples = accountYearPeopleDao.findVoteRecord();
                Result result = new Result(200, "success", yearPeoples);
                resp.getWriter().print(new ObjectMapper().writeValueAsString(result));
            } catch (SQLException e) {
                Result result = new Result(500, "failed", null);
                resp.getWriter().print(new ObjectMapper().writeValueAsString(result));
            }
        }else if("deleteLog".equals(mark)){
            try {
                accountYearPeopleDao.deleteVoteRecord(id,Integer.parseInt(account));
                Result result = new Result(200, "Delete success", null);
                resp.getWriter().print(new ObjectMapper().writeValueAsString(result));
            } catch (SQLException e) {
                Result result = new Result(500, "Delete failed", null);
                resp.getWriter().print(new ObjectMapper().writeValueAsString(result));
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String account = req.getParameter("account");
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String info = req.getParameter("info");
//        开始插入信息
        YearPeopleDao adminDao = new YearPeopleDao();
        YearPeople yearPeople = new YearPeople(name, Integer.parseInt(account), sex, info);
        yearPeople.setCounts(0);
        try {
            adminDao.insertYearPeople(yearPeople);
            Result result = new Result(200, "success", null);
            resp.getWriter().print(new ObjectMapper().writeValueAsString(result));
        } catch (SQLException e) {
            Result result = new Result(500, "failed", null);
            resp.getWriter().print(new ObjectMapper().writeValueAsString(result));
        }
    }
}
